<?php
namespace app\common\logic;

use app\common\model\FlashSale;
use app\common\model\Goods;
use think\Db;

/**
 * 秒杀商品逻辑定义
 * Class CatsLogic
 * @package common\Logic
 */
class FlashSaleLogic extends Prom
{
    protected $FlashSale;//秒杀活动模型
    protected $goods;//商品模型

    public function __construct($goods)
    {
        parent::__construct();
        $this->goods = $goods;

        //活动商品没有规格，活动和规格是一对多
        $this->FlashSale = FlashSale::get($goods['prom_id'],'',true);

        if ($this->FlashSale) {
            //每次初始化都检测活动是否失效，如果失效就更新活动和商品恢复成普通商品
            if ($this->checkActivityIsEnd() && $this->FlashSale['is_end'] == 0) {

                Db::name('goods')->where("id", $this->goods['id'])->update(['prom_type' => 0, 'prom_id' => 0]);

                $this->FlashSale->is_end = 1;
                $this->FlashSale->save();
                unset($this->goods);
                $this->goods = Goods::get($goods['id'],'',true);
            }
        }
    }

    public function getPromModel(){
        return $this->FlashSale;
    }

    /**
     * 活动是否正在进行
     * @return bool
     */
    public function checkActivityIsAble(){
        if(empty($this->FlashSale)){
            return false;
        }
        if(time() > $this->FlashSale['start_time'] && time() < $this->FlashSale['end_time'] && $this->FlashSale['status'] == 1 && $this->FlashSale['is_end'] == 0){
            return true;
        }
        return false;
    }

    /**
     * 活动是否结束
     * @return bool
     */
    public function checkActivityIsEnd(){
        if(empty($this->FlashSale)){
            return true;
        }
        if(time() > $this->FlashSale['end_time']){
            return true;
        }
        return false;
    }

    /**
     * 获取商品原始数据
     * @return Goods
     */
    public function getGoodsInfo()
    {
        return $this->goods;
    }

    /**
     * 该活动是否已经失效
     */
    public function IsAble(){
        if(empty($this->FlashSale)){
            return false;
        }
        if(time() > $this->FlashSale['end_time']){
            return false;
        }
        if($this->FlashSale['status'] != 1){
            return false;
        }
        if($this->FlashSale['is_end'] == 1){
            return false;
        }
        return true;
    }

    /**
     * 获取商品转换活动商品的数据
     * @return static
     */
    public function getActivityGoodsInfo(){
        $activityGoods = $this->goods;
        $activityGoods['activity_title'] = $this->FlashSale['title'];
        $activityGoods['market_price'] = $this->goods['market_price'];
        $activityGoods['start_time'] = $this->FlashSale['start_time'];
        $activityGoods['end_time'] =  $this->FlashSale['end_time'];
        $activityGoods['item_id'] = $this->FlashSale['item_id'];
        $activityGoods['shop_price'] =  $this->FlashSale['price'];
        $activityGoods['description'] =  $this->FlashSale['description'];

        $activityGoods['buy_num'] = $this->FlashSale['buy_num'];
        $activityGoods['goods_num'] = $this->FlashSale['goods_num'];


        return $activityGoods;
    }

    /**
     * 获取用户抢购已购商品数量
     * @param $user_id
     * @return float|int
     */
    public function getUserFlashOrderGoodsNum($user_id){
        $orderWhere = [
            'user_id'=>$user_id,
            'order_status' => ['<>', 3],
            'creatime' => ['between', [$this->flashSale['start_time'], $this->flashSale['end_time']]]
        ];
        $order_id_arr = Db::name('order')->where($orderWhere)->getField('id', true);
        if ($order_id_arr) {
            $orderGoodsWhere = ['prom_id' => $this->flashSale['id'], 'prom_type' => 3, 'id' => ['in', implode(',', $order_id_arr)]];
            $goods_num = DB::name('order')->where($orderGoodsWhere)->sum('goods_num');
            if($goods_num){
                return $goods_num;
            }else{
                return 0;
            }
        } else {
            return 0;
        }
    }


}